# 模块导入
from . import Collect
from . import User
from . import Book
from . import database


# Collect表的mapper
class CollectMapper:
    # query
    # 根据collect_id查找collect项
    @staticmethod
    def query_collect_id(collect_id):
        return Collect.query.get(collect_id)

    # 查找collect列表
    @staticmethod
    def query_all():
        return Collect.query.filter().all()

    # 根据user_id查询collect项
    @staticmethod
    def query_user_id(user_id):
        return Collect.query.filter_by(user_id=user_id).all()

    # 根据username查询collect项
    @staticmethod
    def query_username(username):
        userlist = User.query.filter_by(username=username).all()
        collectlist = []
        for user in userlist:
            collect = Collect.query.filter_by(user_id=user.user_id).first()
            if collect is None:
                break
            collectlist.append(collect)
        return collectlist

    # 根据username模糊查询collect项
    @staticmethod
    def query_username_like(keyword):
        userlist = User.query.filter(User.username.like('%'+keyword+'%')).all()
        collectlist = []
        for user in userlist:
            collect = Collect.query.filter_by(user_id=user.user_id).first()
            if collect is None:
                break
            collectlist.append(collect)
        return collectlist

    # 根据book_id查询collect项
    @staticmethod
    def query_book_id(book_id):
        return Collect.query.filter_by(book_id=book_id).all()

    # 根据bookname查询collect项
    @staticmethod
    def query_book_name(book_name):
        booklist = Book.query.filter_by(book_name=book_name).all()
        collectlist = []
        for book in booklist:
            collect = Collect.query.filter_by(book_id=book.book_id).first()
            if collect is None:
                break
            collectlist.append(collect)
        return collectlist

    # 根据bookname模糊查询collect项
    @staticmethod
    def query_book_name_like(keyword):
        booklist = Book.query.filter(Book.book_name.like('%'+keyword+'%')).all()
        collectlist = []
        for book in booklist:
            collect = Collect.query.filter_by(book_id=book.book_id).first()
            if collect is None:
                break
            collectlist.append(collect)
        return collectlist

    # 根据user_id,book_id查询collect项
    @staticmethod
    def query_user_id_book_id(user_id, book_id):
        return Collect.query.filter_by(user_id=user_id, book_id=book_id).first()

    # insert
    # 增加一个collect项
    @staticmethod
    def insert(user_id, book_id):
        collect = Collect(user_id=user_id, book_id=book_id)
        database.session.add(collect)
        database.session.commit()
        return "insert success"

    # update
    # 更新collect项
    @staticmethod
    def update(collect_id, user_id, book_id):
        collect = Collect.query.get(collect_id)
        collect.user_id = user_id
        collect.book_id = book_id
        database.session.commit()
        return "update success"

    # delete
    # 删除collect项
    @staticmethod
    def delete(collect_id):
        collect = Collect.query.get(collect_id)
        database.session.delete(collect)
        database.session.commit()
        return "delete success"
